package net.maergaiyun.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedissonConfiguration {

    @Value("${spring.redis.host}")
    private String redisHost;

    @Value("${spring.redis.port}")
    private String redisPort;

    @Value("${spring.redis.password}")
    private String redisPwd;

    /**
     * 配置分布式锁的redisson
     *
     * @return
     */
    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        // 单机
        config.useSingleServer().setPassword(redisPwd).setAddress("redis://" + redisHost + ":" + redisPort);

        // 集群
        // config.useClusterServers().addNodeAddress("redis://192.168.1.10:6379", "redis://192.168.1.20:6379");

        RedissonClient redissonClient = Redisson.create(config);
        return redissonClient;
    }

    /**
     * 集群模式
     */
//    @Bean
//    public RedissonClient redissonClusterClient() {
//        Config config = new Config();
//        config.useClusterServers().setScanInterval(2000)  // 集群状态扫描间隔时间, 单位毫秒
//                .addNodeAddress("redis://127.0.0.1:7000")
//                .addNodeAddress("redis://127.0.0.1:7001");
//        RedissonClient redisson = Redisson.create(config);
//        return redisson;
//    }
}
